package com.songcan.gateway.route.controller;


import com.alibaba.fastjson.JSONObject;
import com.songcan.gateway.exception.RouteException;
import com.songcan.gateway.exception.ValidatedExceotion;
import com.songcan.gateway.route.service.CustomRouteDefinitionService;
import com.songcan.gateway.vo.CustomRouteDefinitionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Map;

@RestController
@RequestMapping("/gateway")
public class RouteController {

    private CustomRouteDefinitionService customRouteDefinitionService;

    @Autowired
    public RouteController(CustomRouteDefinitionService customRouteDefinitionService) {
        this.customRouteDefinitionService = customRouteDefinitionService;

    }

    // 新增和修改路由
    @PostMapping("/route")
    public Mono<JSONObject> addRouteDefinition(@Validated Mono<CustomRouteDefinitionVO> customRouteDefinition) {
        CustomRouteDefinitionVO entity  = customRouteDefinitionService.save(customRouteDefinition.block());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","200");
        jsonObject.put("msg","操作成功");
        return Mono.just(jsonObject).doOnError(e->new RouteException("操作异常,"+e.getMessage()));
    }

    // 获取路由
    @GetMapping("/route")
    public Mono<Map<String,CustomRouteDefinitionVO>> getRoteDefinition(){
        return Mono.just(customRouteDefinitionService.findAll());
    }

    // 删除路由
    @DeleteMapping("route/{id}")
    public Mono<JSONObject> removeRouteDefinition(@PathVariable String id) {
        customRouteDefinitionService.deleteById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","200");
        jsonObject.put("msg","操作成功");
        return Mono.just(jsonObject).doOnError(e->new RouteException("操作异常,"+e.getMessage()));
    }

    @PutMapping("/route")
    public Mono<JSONObject> updateRouteDefinition(@NotBlank(message = "id不能为空")String id,
                                            @NotEmpty(message = "uri不能为空")String uri,
                                            @NotEmpty(message = "匹配条件不能为空")String predicates,
                                            String filters,@RequestParam(defaultValue = "0")Integer order,
                                            @RequestParam(defaultValue = "1") Integer enabled) throws ValidatedExceotion {
        customRouteDefinitionService.update(id,uri,predicates,filters,order,enabled);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","200");
        jsonObject.put("msg","操作成功");
        return Mono.just(jsonObject).doOnError(e->new RouteException("操作异常,"+e.getMessage()));
    }

    /**
     * 刷新路由配置
     * @return
     */
    @GetMapping("/refresh")
    public Mono<JSONObject> refresh(){
        customRouteDefinitionService.loadRouteDefinition();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","200");
        jsonObject.put("msg","操作成功");
        return Mono.just(jsonObject).doOnError(e->new RouteException("操作异常,"+e.getMessage()));
    }


}
